package com.xboge.service;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * @Title: MyCustomScanner
 * @Author xboge
 * @Date 2021-06-09 18:31
 * @Description: 自定义ClassPathBeanDefinitionScanner对象，用于扫描工作
 * 用法：
 * AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
 *         //注册配置类
 *         context.register(Config.class);
 *         CustomScanner customScanner = new CustomScanner(context);
 *         customScanner.addIncludeFilter(new AnnotationTypeFilter(MyAnnotation.class));
 *         int num = customScanner.scan("com.scan");
 *         context.refresh();
 */
public class MyCustomScanner  extends ClassPathBeanDefinitionScanner {
	public MyCustomScanner(BeanDefinitionRegistry registry) {
		super(registry);
	}

	@Override
	public void addIncludeFilter(TypeFilter includeFilter) {
		super.addIncludeFilter(includeFilter);
	}

	@Override
	protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
		//如果是接口，则返回true，会添加到IOC容器中去
		return metadataReader.getAnnotationMetadata().isInterface();

	}
}
